/**
 * @param {string} haystack
 * @param {string} needle
 * @return {number}
 */
var strStr = function (haystack, needle) {
  let h = haystack.length;
  let n = needle.length;
  if (n === 0) {
    return 0;
  }
  let dp = [];
  for (let i = 1, j = 0; i < m; i++) {
    while (j > 0 && needle[i] !== needle[j]) {
      j = dp[j - 1];
    }
    if (needle[i] === needle[j]) {
      j++;
    }
    dp[i] = j;
  }
  for (let i = 0, j = 0; i < n; i++) {
    while (j > 0 && haystack[i] != needle[j]) {
      j = dp[j - 1];
    }
    if (haystack[i] == needle[j]) {
      j++;
    }
    if (j == m) {
      return i - m + 1;
    }
  }
  return -1;
};
